//公共
import myHeader from "/pages/headerfoot/pages/header.vue"
//骨架
import uniSkeleton from "/plugin/skeleton/components/heaven-skeleton/heaven-skeleton.vue";
//公共公益方法
import commonBenefit from '@/common/commonbenefit.js';
//公共方法
import Base from '@/common/base.js';
//语言
import Lang from '@/utils/lang.js';
//微信api
import weChat from '@/lib/wechat.js';


export default {
	components: {
		myHeader,
		uniSkeleton,
	},
	data() {
		return {
			information: {},
			projectoption: {}, //所有题目
			nowprojectoption: {}, //当前题目
			numbers: 0, //默认题目
			pagecount: 1, //题目
			allnumbers: 0, //总题
			loading: true, //骨架
			nowselected: '',

			gradingquestionsarr: [], //题目
			questionsanswerarr: [], //正确答案
			myanswerarr: [], //用户填写答案
			articleid: 0, //文章ID
			projectid: 0, //项目id
			readingtime: 1, //阅读时间
			answertime: 1, //答题时间
			timer: {}, //正记时
			correctquantitynumber:0,//正确答个数
			lengths:0,//总提数
			
		}
	},
	onShow() {
		var that = this;
		that.ifreglogin();
		that.countdown();
	},
	 
	onLoad(option) {
		var that = this;
		//重置
		that.gradingquestionsarr = [];
		that.questionsanswerarr = [];
		that.myanswerarr = [];

		//得到项目信息
		var data = {};
		that.projectid = data['project_id'] = option.projectid;
		that.readingtime = option.seconds;
		commonBenefit.getProjectApiMethod(data).then(res => {
			if (res) {
				that.information = res;
				that.allnumbers = res.project_option.length; //总题
				that.nowprojectoption = res.project_option[that.numbers];
				that.articleid = res.project_article.id; //文章ID

				that.loading = false;
			}
		});
	},
	methods: {
		//判断是否需要注册或者登录与其他
		async ifreglogin() {
			var that = this;
			//注册
			await Base.globalauthorization();

			//判断学生是否注册
			await commonBenefit.ifStudenter().then(res => {
				if (res) {
					uni.redirectTo({
						url: commonBenefit.automaticurl('studentreg')
					})
				}
			});
		},
		//下一题
		down() {
			var that = this;
			if (!that.nowselected) {
				Base.publicshowtoasterror(Lang.lang.pleasechoose);
				return false;
			}
			//匹配答案
			var nowproject = that.information.project_option[that.numbers].project_answer[that.nowselected];
			//获取正确ID
			var answer_id = '';
			that.information.project_option[that.numbers].project_answer.forEach((item, index) => { //js遍历数组
				if (item.answer_answer == '1') {
					answer_id = item.value
				}
			});
			//判断正确
			if(nowproject.value==answer_id){
				that.correctquantitynumber++;
			}

			that.gradingquestionsarr.push(that.nowprojectoption.id); //题目
			that.questionsanswerarr.push(answer_id); //正确答案
			that.myanswerarr.push(nowproject.value); //用户填写答案
			//提交
			if (that.pagecount == that.allnumbers) {
				that.submit();
			} else {
				++that.numbers;
				++that.pagecount; //题目
				that.nowselected = '';
				//重新赋值
				that.nowprojectoption = that.information.project_option[that.numbers]
			}


		},
		//正记时
		countdown() {
			var that = this;
			that.timer = setInterval(() => {
				that.answertime = ++that.answertime; //计时器
			}, 1000);
		},
		//提交问题
		submit() {
			var that = this
			//清除定时器
			clearInterval(that.timer);
			var data = {};
			data['grading_questions'] = JSON.stringify(that.gradingquestionsarr); //题目
			data['questions_answer'] = JSON.stringify(that.questionsanswerarr); //正确答案
			data['my_answer'] = JSON.stringify(that.myanswerarr); //用户填写答案
			data['allscore'] =  (parseFloat(that.correctquantitynumber) / parseFloat(that.allnumbers)) * 100; //分数
			data['article_id'] = that.articleid;
			data['project_id'] = that.projectid;
			data['readingtime'] = that.readingtime;
			data['answertime'] = that.answertime;
			data['grading_type'] = 'reading';
			commonBenefit.setUserProjectApiMethod(data).then(res => {
				if (res) {
					var id = res.id;
					var projectanswerend = commonBenefit.automaticurl('projectanswerend') + "?id=" + id;
					uni.redirectTo({
						url: projectanswerend
					})

				}
			});

		},
		//获取选中值
		radioChange: function(e) {
			var that = this
			that.nowselected = e.detail.value
		}
	}
}